﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.Data.Entity;

namespace GPS.Controllers.GPS_DanhMuc
{
    public class GPS_XepHangController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        
        #region Index
        public ActionResult Index(ViewModelSXEPHANG SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            var entities = _db.Filter<SXEPHANG>(o => (SearchModel.qXEPHANG_HANGGPLX_MA == null || o.XEPHANG_HANGGPLX_MA.StartsWith(SearchModel.qXEPHANG_HANGGPLX_MA))
                                        && (SearchModel.qXEPHANG_ACTIVE == null || o.XEPHANG_ACTIVE.StartsWith(SearchModel.qXEPHANG_ACTIVE)));
            ViewBag.TotalRow = entities.Count();
            var model = new List<SXEPHANG>();
            foreach (var currentEntity in entities)
            {
                model.Add(Mapper.Map<SXEPHANG>(currentEntity));
            }
            Int16 icount = 1;
            foreach (var item in model)
            {
                item.STT = icount;
                icount++;
            }
            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return View(SearchModel);
        }

        [HttpPost]
        public void initialCategorySearchAction(ViewModelSXEPHANG viewModel)
        {
            ViewBag.lstHangGPLX = new SelectList(_db.GetAll<SHANGGPLX>(), "HGPLX_MA", "HGPLX_TEN");
            //viewModel.listTrangThai = _db.GetAll<STRANGTHAI>();
        }
        #endregion

        #region Detail
        public ActionResult Detail(decimal id)
        {
            CrudModelSXepHang _model = new CrudModelSXepHang();
            SXEPHANG model = _db.GetOne<SXEPHANG>(o => o.ID == id);
            GPS.COMMON.Helpers.CopyObject<CrudModelSXepHang>(model, ref _model);
            initialCategoryDetailAction(_model);
            return PartialView("Detail", _model);
        }

        public void initialCategoryDetailAction(CrudModelSXepHang view)
        {
            if (view.XEPHANG_ACTIVE == "1")
            {
                view.XEPHANG_ACTIVE = "Hiệu Lực";
            }
            else
            {
                view.XEPHANG_ACTIVE = "Không Hiệu Lực";
            }
        }
        #endregion
    }
}